package Effects.Debuffs;

import Effects.Effect;
import Effects.Buffs.Buff;
import Entities.RPGEntity;

/**
 * @author Hetalar
 * 09/02/2012
 */
public abstract class Debuff implements Effect
{
	//Properties
	protected int ID;
	protected String name;
	protected double power;
	protected RPGEntity host;
	protected boolean applied;
	protected boolean mergeable;
	
	/**
	 * Constructor for Buff
	 * @param incID ID for Buff
	 */
	protected Debuff(String buffName, double power, int ID, RPGEntity host)
	{
		this.ID = ID;
		this.name = buffName;
		this.power = power;
		this.host = host;
		this.applied = false;
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#isApplied()
	 */
	public boolean isApplied()
	{
		return applied;
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#getPower()
	 */
	public double getPower()
	{
		return power;
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#setPower(double)
	 */
	public void setPower(double newPower)
	{
		removeEffect();
		power = newPower;
		applyEffect();
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#hasHost()
	 */
	public boolean hasHost()
	{
		return host != null;
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#setHost(Entities.RPGEntity)
	 */
	public void setHost(RPGEntity host)
	{
		//If this buff already has a host - don't assign it a new one.
		if(!hasHost())
		{
			this.host = host;
		}
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#getID()
	 */
	public int getID()
	{
		return this.ID;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other)
	{
		boolean result = false;
		
		if(other instanceof Buff)
		{
			Buff b = (Buff)other;
			result = (this.getKey().equals(b.getKey()));
		}
			
		return result;
	}
	
	
	/* (non-Javadoc)
	 * @see Effects.Effect#merge(Effects.Effect)
	 */
	public void merge(Effect other)
	{
		//Ensuring it is actually 2 of the same type of effects being merged
		if(other.getClass() == this.getClass())
		{
			//they are the same
			setPower(getPower() + other.getPower());
			if(other.hasHost())
			{
				((Debuff)other).host.removeEffect(other);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#getKey()
	 */
	public String getKey()
	{
		String result = String.valueOf(this.ID) + String.valueOf(this.power);
		result += this.name + String.valueOf(this.applied);
		
		return result;
	}
	
	/* (non-Javadoc)
	 * @see Effects.Effect#isMergeable()
	 */
	public boolean isMergeable()
	{
		return mergeable;
	}
}
